<?php

namespace App\Http\Controllers\Alipay;

use App\Models\Address;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class AddressController extends BaseController
{
    private $addressData;
    private $uid;
    private $request;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->addressData = new Address();
//        $this->uid = 1;
        $this->uid = $this->member['id'];
    }

    /**
     * 地址列表
     * @param $uid
     * @return false|string
     */
    public function addressList()
    {
        $where = [
            ['uid', $this->uid],
            ['is_del', Address::NODELETE]
        ];
        $data = $this->addressData->where($where)->orderByDesc('id')->get();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 新增地址
     * @param Request $request
     * @return false|string.
     */
    public function addAddress()
    {
        $request_data = $this->request->post();
        $validator = Validator::make($request_data, [
            'province' => 'required',
            'city' => 'required',
            'area' => 'required',
            'address' => 'required',
            'phone' => ['required','regex:/^1[2-9]\d{9}$/'],
            'name' => 'required',
            'is_default' => ['required', Rule::in([0, 1])],
        ]);
        if ($validator->fails()) return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请完善信息');
        if ($request_data['is_default'] == Address::DEFAULT) {
            $this->addressData->where('uid', $this->uid)->update(['is_default' => Address::NODEFAULT]);
        }
        $request_data['uid'] = $this->uid;
        $result = $this->addressData->create($request_data);
        if ($result) return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '新增成功');
        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '新增失败');
    }

    /**
     * 地址详情
     * @param $id
     * @param $uid
     * @return false|string
     */
    public function detailAddress()
    {
        $id = $this->request->get('id', 0);
        if (empty($id)) return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数错误');
        $where = [
            ['uid', $this->uid],
            ['id', $id]
        ];
        $data = $this->addressData->where($where)->first();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 编辑地址
     * @param Request $request
     * @return false|string
     */
    public function editAddress()
    {
        $request_data = $this->request->post();
        $validator = Validator::make($request_data, [
            'id' => 'required',
            'province' => 'required',
            'city' => 'required',
            'area' => 'required',
            'address' => 'required',
            'phone' => ['required','regex:/^1[2-9]\d{9}$/'],
            'name' => 'required',
            'is_default' => ['required', Rule::in([0, 1])],
        ]);
        if ($validator->fails()) return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请完善信息');
        $where = [
            ['uid', $this->uid],
            ['id', $request_data['id']]
        ];
        unset($request_data['id']);
        if ($request_data['is_default'] == Address::DEFAULT) {
            $this->addressData->where('uid', $this->uid)->update(['is_default' => Address::NODEFAULT]);
        }
        if ($this->addressData->where($where)->update($request_data)) return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '修改成功');
        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '修改失败');
    }

    /**
     * 删除地址
     * @param Request $request
     * @return false|string
     */
    public function deleteAddress()
    {
        $id = $this->request->get('id', 0);
        if (empty($id)) return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数错误');
        $where = [
            ['uid', $this->uid],
            ['id', $id]
        ];
        $update = [
            'is_del' => Address::DELETE
        ];
        if ($this->addressData->where($where)->update($update)) return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '删除失败');
    }

}
